package com.xiaohong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.file.feign.FileFeign;
import com.xiaohong.user.dao.UserMapper;
import com.xiaohong.user.pojo.User;
import com.xiaohong.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FileFeign fileFeign;

    /**
     * 添加用户积分
     * @param point
     */
    @Override
    public void addPoints(String username,Integer point) {
        userMapper.addPoints(username,point);
    }

    /**
     * 更新用户头像
     * @param file
     */
    @Override
    public String updateHeadPic(MultipartFile file,Long id) {
        //判断用户头像是否为空
        User user =this.findById(id);
        if (!StringUtils.isEmpty(user.getHeadPic())){
            //不为空先删除再添加
            fileFeign.deleteFile(user.getHeadPic());
        }
        //更新头像
        String url = (String) fileFeign.uploadFile(file).getData();
        //将头像地址存储到数据库中(创建一个新的User)
        User user1 = new User();
        user1.setId(user.getId());
        user1.setHeadPic(url);
        // 更新头像(只填入id和头像地址)
        this.update(user1);
        return url;
    }

    /**
     * User条件+分页查询
     * @param user 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<User> findPage(User user, int page, int size){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(user);
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return userMapper.selectPage(pg, queryWrapper);
    }

    /**
     * User分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<User> findPage(int page, int size){
        // 构建分页
        Page pg = new Page(page, size);
        // 分页查询
        return userMapper.selectPage(pg, null);
    }

    /**
     * User多条件搜索方法
     * @param User
     * @return
     */
    @Override
    public List<User> findList(User User){
        // 构建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>(User);
        // 查询数据
        return userMapper.selectList(queryWrapper);
    }

    /**
     * 根据主键删除
     * @param id
     */
    @Override
    public void delete(Long id){
        userMapper.deleteById(id);
    }

    /**
     * 修改User
     * @param user
     */
    @Override
    public void update(User user){
        userMapper.updateById(user);
    }

    /**
     * 根据主键修改User
     * @param user
     */
    @Override
    public void updateById(User user){
        userMapper.updateById(user);
    }

    /**
     * 增加User
     * @param user
     */
    @Override
    public void add(User user){
        userMapper.insert(user);
    }

    /**
     * 根据ID查询User
     * @param id
     * @return
     */
    @Override
    public User findById(Long id){
        return  userMapper.selectById(id);
    }

    /**
     * 查询User全部数据
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

}
